package dao.studentworkdao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import util.DateTool;

import dao.basedao.DBManager;
import model.StudentWork;

public class StudentWorkDaoImpl implements StudentWorkDao {
	@Override
	public boolean save(StudentWork stuwork) throws Exception, ClassNotFoundException, Exception {
        boolean flag =false;
		
        Connection con=DBManager.getConn();
		PreparedStatement ps=con.prepareStatement("insert into submitwork(work_id,student_id,submittime,filepath) values (?,?,?,?)");
		ps.setInt(1, stuwork.getWork_id());
		ps.setString(2,stuwork.getStu_id());
		java.sql.Date submittime=DateTool.JavaDatetoSqlDate(stuwork.getSubmittime());
		ps.setDate(3, submittime);
		ps.setString(4,stuwork.getFilePath().replaceAll("\\\\", "/"));
		int rs=ps.executeUpdate();
		if(rs>0)
			flag=true;
		ps.close();
		DBManager.closeConn(con);
		return flag;
	}

	@Override
	public String querysubmitState(int work_id, String stu_id) throws Exception, ClassNotFoundException, Exception {
		String result="no";
	    Connection con=DBManager.getConn();
		PreparedStatement ps=con.prepareStatement("select * from submitwork where work_id=? and student_id=?");
		ps.setInt(1, work_id);
		ps.setString(2, stu_id);
		ResultSet rs=ps.executeQuery();
		int count=0;
		while(rs.next()){
			count++;
		}
		if(count>0){
			result="yes";
		}
		rs.close();
		ps.close();
		DBManager.closeConn(con);
		return result;
	}

}
